import time

from selenium.webdriver.common.by import By
from common.Utils import ForumDriver

class ForumLogin:
    driver = ""
    url = ""
    def __init__(self):
        self.driver = ForumDriver.driver
        self.url = "http://127.0.0.1:9580/sign-in.html"
        self.driver.get(self.url)

    def ForumSucLoginTest(self):
        #找到用户名输入框，在用户名输入框输入正常的用户名
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("rxw")
        #找到密码输入框，在密码输入框输入正常的密码
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        time.sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(2)
        ForumDriver.GetScreeShot()

        # http://127.0.0.1:9580/index.html
        #判断一下登录后的url，页面标题
        LoginURL = self.driver.current_url
        Logintitle = self.driver.title
        print("LoginURL"+LoginURL)
        print("Logintitle"+Logintitle)
        assert LoginURL == "http://127.0.0.1:9580/index.html"
        assert Logintitle == "比特论坛"
        #检查一下页面元素存在
        #用户头像
        self.driver.find_element(By.CSS_SELECTOR,"#index_nav_avatar")
        #论坛标识
        self.driver.find_element(By.CSS_SELECTOR, "body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > h1 > a > img")
        #首页标识
        self.driver.find_element(By.CSS_SELECTOR,"#article_list_board_title")
        #发贴按钮
        self.driver.find_element(By.CSS_SELECTOR,"#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")
        #帖子列表页，的用户头像
        self.driver.find_element(By.CSS_SELECTOR, "#artical-items-body > div:nth-child(1) > div > div.col-auto > span")
        #帖子列表的标题
        self.driver.find_element(By.CSS_SELECTOR, "#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")
        #帖子列表的帖子的发布者
        postuser = self.driver.find_element(By.CSS_SELECTOR, "#artical-items-body > div:nth-child(1) > div > div.col > div.text-muted.mt-2 > div > div.col > ul > li:nth-child(1)").text
        print("postuser"+postuser)
        #帖子列表的帖子的发布时间
        self.driver.find_element(By.CSS_SELECTOR, "#artical-items-body > div:nth-child(1) > div > div.col > div.text-muted.mt-2 > div > div.col > ul > li:nth-child(2)")

        # self.driver.quit()


    def ForumFailLoginTest(self):
        self.driver.back()


        # 找到用户名输入框，在用户名输入框输入正常的用户名,错误的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("rxw")
        # 找到密码输入框，在密码输入框输入正常的密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(3)
        ForumDriver.GetScreeShot()

        time.sleep(1)
        # 找到用户名输入框，在用户名输入框输入错误的用户名,正常的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("rxwww")
        # 找到密码输入框，在密码输入框输入正常的密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1234567")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        ForumDriver.GetScreeShot()

        time.sleep(1)
        # 找到用户名输入框，在用户名输入框输入错误的用户名,错误的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("rxwww")
        # 找到密码输入框，在密码输入框输入正常的密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        ForumDriver.GetScreeShot()

        time.sleep(1)
        # 找到用户名输入框，在用户名输入框不输入用户名,输入错误的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("")
        # 找到密码输入框，在密码输入框输入正常的密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        ForumDriver.GetScreeShot()


        time.sleep(1)
        # 找到用户名输入框，在用户名输入框输入正确用户名,不输入密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("rxw")
        # 找到密码输入框，在密码输入框输入正常的密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        ForumDriver.GetScreeShot()

        time.sleep(1)
        # 找到用户名输入框，在用户名输入框不输入用户名,不输入密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("")
        # 找到密码输入框，在密码输入框输入正常的密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        ForumDriver.GetScreeShot()

        self.driver.quit()




# login=ForumLogin()
# login.ForumSucLoginTest()
# login.ForumFailLoginTest()


